/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre.sample.slice;

import com.andremion.louvre.sample.PupUpCallBack;
import com.andremion.louvre.sample.ResourceTable;
import com.andremion.louvre.sample.util.ResTUtil;
import com.andremion.louvre.sample.util.UiTUtil;
import ohos.agp.components.*;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.app.Context;

/**
 * AlertDialog Class.
 */
public class ChooseImageLimitDialog extends CommonDialog implements IDialog {
    private static final String TAG = ChooseImageLimitDialog.class.getSimpleName();
    private static PupUpCallBack mPupUpCallBack;
    final Builder builder;
    int maxlimit = 1;

    private ChooseImageLimitDialog(Builder builder) {
        super(builder.getContext());
        this.builder = builder;
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(builder.getContext())
                .parse(getInflateLayout(builder), null, false);
        prepareDialogViewButton(rootLayout, builder);
        setContentCustomComponent(rootLayout);
    }

    /**
     * setPupUpCallBack
     *
     * @param mPupUpCallBack CallBack
     */
    public void setPupUpCallBack(PupUpCallBack mPupUpCallBack) {
        this.mPupUpCallBack = mPupUpCallBack;
    }

    /**
     * Builder Class.
     */
    public static class Builder {
        CharSequence title;
        Picker picker;
        private Context context;

        public Builder(Context context) {
            this.context = context;
        }

        public final Context getContext() {
            return context;
        }

        /**
         * title
         *
         * @param titleRes title resource id
         * @return Builder
         */
        public Builder title(int titleRes) {
            this.title = ResTUtil.getString(context, titleRes);
            return this;
        }

        /**
         * setPickerView
         *
         * @param pickerComp pickerComp
         * @return Builder
         */
        public Builder setPickerView(Picker pickerComp) {
            picker = pickerComp;
            return this;
        }


        /**
         * show show ChooseImageLimitDialog
         *
         * @return ChooseImageLimitDialog
         */
        public ChooseImageLimitDialog show() {
            return new ChooseImageLimitDialog(this);
        }
    }

    private int getInflateLayout(Builder builder) {
        return ResourceTable.Layout_alert_dialog_layout;
    }

    private void prepareDialogViewButton(ComponentContainer rootLayout, Builder builder) {
        Text tvTitle = UiTUtil.getComponent(rootLayout, ResourceTable.Id_title);
        DirectionalLayout titleFrame = UiTUtil.getComponent(rootLayout, ResourceTable.Id_titleFrame);
        DirectionalLayout contentView = UiTUtil.getComponent(rootLayout, ResourceTable.Id_content);
        Button okButton = UiTUtil.getComponent(rootLayout, ResourceTable.Id_limit_ok);
        Button cancelButton = UiTUtil.getComponent(rootLayout, ResourceTable.Id_limit_cancel);
        // Setup title and title frame
        if (tvTitle != null) {
            tvTitle.setTextAlignment(TextAlignment.START);

            if (builder.title == null) {
                if (titleFrame != null) {
                    titleFrame.setVisibility(Component.HIDE);
                }
            } else {
                tvTitle.setText(builder.title.toString());
                if (titleFrame != null) {
                    titleFrame.setVisibility(Component.VISIBLE);
                }
            }
        }

        if (contentView != null) {
            if (builder.picker != null) {
                builder.picker.setValueChangedListener((picker, oldValue, newValue) -> {
                    maxlimit = newValue;
                });
                contentView.addComponent(builder.picker);
            } else {
                contentView.setVisibility(Component.HIDE);
            }
        }

        okButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mPupUpCallBack.numberPickCallBack(maxlimit);
                destroy();
            }
        });

        cancelButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                destroy();
            }
        });
    }
}
